﻿@{
    ViewData["Title"] = "Home";
}

<div>
    <div id="liveAlertPlaceholder"></div>
    <form action="/" method="post">
        <div class="mb-3">
            <label for="name" class="form-label">剧集名称</label>
            <input type="text" class="form-control form-control-sm" id="name" placeholder="剧集名称">
        </div>
        <div class="mb-3">
            <label for="start_num" class="form-label">起始集数序号</label>
            <input type="number" class="form-control form-control-sm" id="start_num" placeholder="起始集数序号">
        </div>
        <div class="mb-3">
            <label for="urls" class="form-label">下载地址</label>
            <textarea class="form-control form-control-sm" id="urls" placeholder="下载地址" rows="5"></textarea>
        </div>
        <button type="submit" class="btn btn-primary">提交</button>
    </form>
</div>

<script>
    document.querySelector('form').addEventListener('submit', function (e) {
        //阻止表单提交
        e.preventDefault();
        e.stopPropagation();
        const name = document.querySelector('#name').value;
        const urls = document.querySelector('#urls').value;
        // urls根据\r或\n进行分割，分割前判空
        if(urls === '' || urls === undefined || urls === null ) {
            alert('请输入下载地址');
            return;
        }
        const urls_arr = urls.split(/\r|\n/);
        let start_num = document.querySelector('#start_num').value;
        if(start_num === '' || start_num === undefined || start_num === null ) {
            start_num = 1;
        }
        const data = {
            name: name,
            urls: urls_arr,
            start_num: start_num
        };
        console.log(data);
        fetch('/down/task', {
            method: 'PUT',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(data)
        })
        .then(response => response.json())
        .then(data => {
            console.log(data)
            if(data.code === 0){
                alert(data.msg)
            }else{
                alert(data.msg);
            }
        })
    });
</script>